package com.example.lib_repair.detail

import androidx.lifecycle.viewModelScope
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class DetailViewModel : BaseViewModel<DetailRepository,DetailState>() {
    override fun createRepo(): DetailRepository {
        return DetailRepository()
    }
    override fun createState(): DetailState {
//        var list : ArrayList<DetailEntity> = ArrayList()
        return DetailState(ResponseEntity(0,"", DetailEntity()),
                            ResponseEntity(0,"", AddEntity()),
                            ResponseEntity(0,"", AddEntity()),
                            ResponseEntity(0,"", ImgEntity()))
    }


    fun requestAdd(map:HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestAddRepair(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity1=it
                    ) }
                }
        }
    }
    fun requestDetail(map:HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestDetailRepair(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity=it
                    ) }
                }
        }
    }

    fun requestComplete(map:HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestCompleteRepair(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity2=it
                    ) }
                }
        }
    }

    fun requestAddImg(map:HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestAddRepairImg(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity3=it
                    ) }
                }
        }
    }

}